package com.hbuzzs.shiro.controller;

import com.hbuzzs.shiro.config.shiro.ShiroMD5;
import com.hbuzzs.shiro.entity.request.AddUserRequest;
import com.hbuzzs.shiro.entity.request.UserLoginRequest;
import com.hbuzzs.shiro.service.IUserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangzhishun
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final IUserInfoService userInfoService;

    @PostMapping("/login")
    public String login(@RequestBody UserLoginRequest user) {

        if (StringUtils.isEmpty(user.getUserId()) || StringUtils.isEmpty(user.getUserPasswd())) {
            return "请输入用户名和密码！";
        }
        //用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                user.getUserId().toString() + "&&" + user.getRoleName(),
                // 将前端传过来的使用md5加密 使用昵称加盐加密
                ShiroMD5.shiroMD5(userInfoService.getById(user.getUserId()).getUserName(), user.getUserPasswd())
        );
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
        } catch (UnknownAccountException e) {
            log.info("用户名不存在！");
            return "用户名不存在！";
        } catch (AuthenticationException e) {
            log.info("账号或密码错误！");
            return "账号或密码错误！";
        } catch (AuthorizationException e) {
            log.info("没有权限！");
            return "没有权限";
        }
        return "login success";
    }


    /**
     * 仅管理员角色可以访问
     */
    @RequiresRoles("admin_role")
    @GetMapping("/admin_role")
    public String adminRole() {
        return "adminRole success!";
    }

    /**
     * 仅用户角色可以访问
     */
    @RequiresRoles("user_role")
    @GetMapping("/user_role")
    public String userRole() {
        return "userRole success!";
    }

    /**
     * 默认所有角色均有init权限
     */
    @RequiresPermissions("init")
    @GetMapping("/init")
    public String init() {
        return "init success!";
    }

    /**
     * 拥有增加数据权限可以访问
     */
    @RequiresPermissions("add")
    @PostMapping("/add")
    @Transactional(rollbackFor = {Exception.class})
    public String add(@RequestBody AddUserRequest request) {
        int id = userInfoService.insertUser(request);
        return "add success! 您的登录ID为：" + id;
    }

    /**
     * 拥有删除数据权限可以访问
     */
    @RequiresPermissions("delete")
    @GetMapping("/delete")
    public String delete() {
        return "delete success!";
    }

    /**
     * 拥有修改数据权限可以访问
     */
    @RequiresPermissions("update")
    @GetMapping("/update")
    public String update() {
        return "update success!";
    }

    /**
     * 拥有查询数据权限可以访问
     */
    @RequiresPermissions("query")
    @GetMapping("/query")
    public String query() {
        return "query success!";
    }


}
